package com.ilvyou.api.common;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by GuanYuCai on 2016/9/7 0007.
 */
public class Cache {
    private static Map<String, User> tokens = new HashMap<String, User>();
    private static Map<String, Validation> validations = new HashMap<String, Validation>();

//    static{
//        User u = new User();
//        u.actId = 2016080118132210090l;
//        u.isComplete = true;
//        tokens.put("abc", u);
//    }

    public static void put(String token, User user){
        if (user.loginTime == 0){
            user.loginTime = System.currentTimeMillis();
        }

        for(Map.Entry<String, User> e : tokens.entrySet()){
            if (e.getValue().actId == user.actId){
                tokens.remove(e.getKey());
                break;
            }
        }
        tokens.put(token, user);
    }

    public static User get(String token){
        return tokens.get(token);
    }

    public static void putValidation(String uuid, Validation v){
        validations.put(uuid, v);
    }

    public static Validation getValidation(String uuid){
        return validations.get(uuid);
    }

    public static void removeValidation(String uuid){
        validations.remove(uuid);
    }

    public static class User{
        public long actId;
        public String source;
        public long loginTime;
        public boolean isComplete;
    }

    public static class Validation{
        public long deadline;
        public int type;
        public String mobile;

        public Validation(String mobile, long deadline, int type){
            this.mobile = mobile;
            this.deadline = deadline;
            this.type = type;
        }
    }
}
